//
//  ReVerbViewController.m
//  ReVerb
//
//  Created by Brandon Braley on 8/21/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ReVerbViewController.h"

@implementation ReVerbViewController

/** The Views */
@synthesize m_pGameScreen;
@synthesize m_pMenuScreen;

/** Backgrounds */
@synthesize m_BG_Main;
@synthesize m_BG_Top;
@synthesize m_BG_Bottom;

/** Buttons */
@synthesize m_btn_Start;
@synthesize m_btn_Exit;

/** HUD */
@synthesize m_lbl_Score;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

	CGPoint Position;
	for (int i=0; i<NUM_LIVES; ++i)
	{
		m_Lives[i] = [rvSprite alloc];
		[m_Lives[i] LoadImage:@"points_1.png" TheView:m_pGameScreen];
		m_Lives[i].m_pImage.bounds = CGRectMake(0, 0, 16, 16);
		//m_Lives[i].m_pImage.center = CGPointMake(16 + (16*i), 16);
		Position.y = 16;
		Position.x = 64 + (24*i);
		m_Lives[i].m_pntPosition = Position;
		
		[m_Lives[i] ApplyPosition];
	}	
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	/** Backgrounds */
	[m_BG_Main release];
	[m_BG_Top release];
	[m_BG_Bottom release];
	
	/** Buttons */
	[m_btn_Start release];
	[m_btn_Exit release];
	
	for ( int i=0; i<NUM_LIVES; ++i )
		[m_Lives[i] release];

    [super dealloc];
}

- (void)PreBeginPlay
{
}

// Returns true if the object has completed the transition
- (BOOL)SlideObjectUp:(UIView*)obj Delta:(float)delta
{
	float height = obj.bounds.size.height;

	// Translate the object in the Y
	obj.center = CGPointMake(obj.center.x, obj.center.y + delta);
	
	float TopY = obj.center.y - (height/2);

	if (delta > 0.0f)
		return TopY >= 0;

	return (TopY + height) <= 0;
}

// Returns true if the object has completed the transition
- (BOOL)SlideObjectDown:(UIView*)obj Delta:(float)delta
{
	float height = obj.bounds.size.height;
	CGRect MyScreen = [[UIScreen mainScreen] bounds];
	
	// Translate the object in the Y
	obj.center = CGPointMake(obj.center.x, obj.center.y + delta);
	
	float TopY = obj.center.y - (height/2);
	
	if (delta > 0.0f)
		return (TopY) >= MyScreen.size.height;
	
	return (TopY + height) <= MyScreen.size.height;
}

- (void)TickMenuAnimation:(NSTimer*)timer
{
	BOOL bDone = TRUE;
	float fDelta = 5;

	if ( ![self SlideObjectUp: m_BG_Top Delta:-fDelta] )
		bDone = FALSE;

	if ( ![self SlideObjectDown: m_BG_Bottom Delta:fDelta] )
		bDone = FALSE;
	
	if ( ![self SlideObjectDown: m_btn_Start Delta:fDelta] )
		bDone = FALSE;
	
	if ( ![self SlideObjectDown: m_btn_Exit Delta:fDelta] )
		bDone = FALSE;
	
	
	if (bDone)
	{
		[timer invalidate];
		[NSTimer scheduledTimerWithTimeInterval:DELTA_FRAME_TIME target:self selector:@selector(TickGamePlay:) userInfo:nil repeats:YES];
	
	}
}

- (void)TickGamePlay:(NSTimer*)timer
{
	for (int i=0; i<NUM_LIVES; ++i)
	{
		[m_Lives[i] Update:DELTA_FRAME_TIME];
	}
}

- (IBAction)OnStartGamePushed:(id)sender
{
	[NSTimer scheduledTimerWithTimeInterval:DELTA_FRAME_TIME target:self selector:@selector(TickMenuAnimation:) userInfo:nil repeats:YES];
	
	// Set up
	m_nScore = 0;
}

- (IBAction)OnExitGamePushed:(id)sender
{
	NSLog(@"Don't quit in ios4, move to a suspended state!");
}

@end
